﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TelescopeScripting.Controls
{
    /// <summary>
    /// Interaction logic for CreateCatalogControl.xaml
    /// </summary>
    public partial class CreateCatalogControl : UserControl
    {
        private string catalogPath;
        public CreateCatalogControl(string rtguiPath)
        {
            InitializeComponent();
            catalogPath = System.IO.Path.GetDirectoryName(rtguiPath);
            GetCatalogsFromFile(catalogPath);
        }

        private void GetCatalogsFromFile(string catPath)
        {
            if (Directory.Exists(catPath))
           {
               string[] catalogs = Directory.GetFiles(catPath, "*.rtg");
               CatalogCombo.ItemsSource = catalogs;
           }
        }

        private void CreatePlayList_Click_1(object sender, RoutedEventArgs e)
        {
            if (CataLogName.Text.Equals(""))
            {
                MessageBox.Show("You need to name your catalog!");
                return;
            }

            string catalogText = "";
            foreach (Object obj in PlayList.Items)
            {
                string catalogObject = obj as string;
                if (catalogObject != null && catalogObject != "")
                {
                    catalogText += catalogObject + "\r\n";
                }
            }

            File.WriteAllText(catalogPath + "\\" + CataLogName.Text + ".rtg", catalogText);
            MessageBox.Show("Created the catalog " + catalogPath + "\\" + CataLogName.Text + ".rtg");
            GetCatalogsFromFile(catalogPath); //refresh the catalogs list
        }

        private void AddObject_Click_1(object sender, RoutedEventArgs e)
        {
            PlayList.Items.Add(SearchList.SelectedItem);
        }

        private void RemoveObject_Click_1(object sender, RoutedEventArgs e)
        {
            if (this.PlayList.Items.Count > 0 && this.PlayList.SelectedIndex >= 0)
            {
                int currentSelected = this.PlayList.SelectedIndex;
                this.PlayList.Items.RemoveAt(this.PlayList.SelectedIndex);
                if (this.PlayList.Items.Count > 0)
                {
                    this.PlayList.SelectedIndex = currentSelected++;
                }
            }
        }

        private void CatalogCombo_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            ComboBox catalogName = sender as ComboBox;
            string catalog = catalogName.SelectedItem as string;
            PopulateCatalog(catalog);
        }

        private void AddAllObjects_Click_1(object sender, RoutedEventArgs e)
        {
            foreach(Object obj in SearchList.Items)
            {
                string catalogObject = obj as string;
                if (catalogObject != null && catalogObject != "")
                {
                    PlayList.Items.Add(catalogObject);
                }
            }
        }

        private void PopulateCatalog(string catalog)
        {
            try
            {
                Regex pattern = new Regex(SearchBox.Text);
                while (SearchList.Items.Count > 0)
                {
                    SearchList.Items.RemoveAt(0);
                }

                if (File.Exists(catalog))
                {
                    using (StreamReader reader = new StreamReader(catalog))
                    {
                        while (!reader.EndOfStream)
                        {
                            string line = reader.ReadLine();
                            if ((pattern.IsMatch(line) || SearchBox.Text.Equals("")) && !line.Equals(""))
                            {
                                SearchList.Items.Add(line);
                            }
                        }
                    }
                }
            }
            catch
            {
                return;
            }
          
        }

        private void SearchBox_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            PopulateCatalog(this.CatalogCombo.SelectedItem as string);
        }

        private void RemoveAllButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (this.PlayList.Items.Count > 0)
            {
                while (this.PlayList.Items.Count > 0)
                {
                    this.PlayList.Items.RemoveAt(0);
                }
            }
        }

        private void UpButton_Click_1(object sender, RoutedEventArgs e)
        {
            int currentIndex = this.PlayList.SelectedIndex;

            if (currentIndex > 0)
            {
                string toMoveUp = this.PlayList.SelectedItem.ToString();
                this.PlayList.Items.RemoveAt(currentIndex);
                this.PlayList.Items.Insert(currentIndex - 1, toMoveUp);
                this.PlayList.SelectedIndex = currentIndex - 1;
            }
        }

        private void DownButton_Click_1(object sender, RoutedEventArgs e)
        {
            int currentIndex = this.PlayList.SelectedIndex;
            if (currentIndex < this.PlayList.Items.Count - 1)
            {
                string toMoveDown = this.PlayList.SelectedItem.ToString();
                this.PlayList.Items.RemoveAt(currentIndex);
                this.PlayList.Items.Insert(currentIndex + 1, toMoveDown);
                this.PlayList.SelectedIndex = currentIndex + 1;
            }
        }

        private void CataLogName_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            if (!CataLogName.Text.All(char.IsLetterOrDigit))
            {
                CataLogName.Background = Brushes.Red;
                CreatePlayList.IsEnabled = false;
            }
            else
            {
                CataLogName.Background = Brushes.White;
                CreatePlayList.IsEnabled = true;
            }

        }
    }
}
